/*
 *所有关于Ads_TimeoutAlert类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*Ads_TimeoutAlertService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using DGZImp.Core.BaseProvider;
using DGZImp.Core.Extensions.AutofacManager;
using DGZImp.AdsProject.DomainModels;
using System.Linq;
using DGZImp.Core.Utilities;
using System.Linq.Expressions;
using DGZImp.Core.Extensions;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using DGZImp.AdsProject.IRepositories;
using System.Net;
using DGZImp.Core.DbSqlSugar;

namespace DGZImp.AdsProject.Services
{
    public partial class Ads_TimeoutAlertService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IAds_TimeoutAlertRepository _repository;//访问数据库
        WebResponseContent webResponse = new WebResponseContent();

        [ActivatorUtilitiesConstructor]
        public Ads_TimeoutAlertService(
            IAds_TimeoutAlertRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        public override WebResponseContent Add(SaveModel saveDataModel)
        {
            // 在保存数据库前的操作，所有数据都验证通过了，这一步执行完就执行数据库保存
            AddOnExecuting = (Ads_TimeoutAlert timeoutAlert, object list) =>
            {
                if (timeoutAlert.AbnormalTypeId == 0)
                {
                    return webResponse.Error($"保存失败，需要有异常类型id（id:{timeoutAlert.AbnormalTypeId}）！");
                }

                if (string.IsNullOrWhiteSpace(timeoutAlert.Post))
                {
                    return webResponse.Error($"保存失败，需要有岗位信息！");
                }
                if (timeoutAlert.TimeoutAlertMinutes <= 0)
                {
                    return webResponse.Error($"保存失败，时间间隔应当大于0分钟！");
                }
                return webResponse.OK();
            };

            return base.Add(saveDataModel);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public override WebResponseContent Update(SaveModel saveModel)
        {
            //此处saveModel是从前台提交的原生数据，可对数据进修改过滤
            UpdateOnExecuting = (Ads_TimeoutAlert timeoutAlert, object addList, object updateList, List<object> delKeys) =>
            {
                if (timeoutAlert.AbnormalTypeId == 0)
                {
                    return webResponse.Error($"保存失败，需要有异常类型id（id:{timeoutAlert.AbnormalTypeId}）！");
                }

                if (string.IsNullOrWhiteSpace(timeoutAlert.Post))
                {
                    return webResponse.Error($"保存失败，需要有岗位信息！");
                }
                if (timeoutAlert.TimeoutAlertMinutes <= 0)
                {
                    return webResponse.Error($"保存失败，时间间隔应当大于0分钟！");
                }
                return webResponse.OK();
            };

            return base.Update(saveModel);
        }

        /// <summary>
        /// 通过异常类型获取该超时配置
        /// </summary>
        /// <param name="abnormalId"></param>
        /// <returns></returns>
        public async Task<TimeoutAlertDto> GetTimeoutAlertByAbnormalId(long abnormalId)
        {
            var result = await Db.Set<Ads_TimeoutAlert>()
                .Where(t => t.IsValid == 1 && t.AbnormalTypeId == abnormalId)
                .Select(t => new TimeoutAlertDto()
                {
                    AbnormalTypeId = t.AbnormalTypeId,
                    Post = t.Post,
                    TimeoutAlertMinutes = t.TimeoutAlertMinutes
                })
                .FirstOrDefaultAsync();
            return result;
        }
    }
}
